package ro.sergiu.finder.models.finder;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import ro.sergiu.finder.filters.FilterPanelUI;
import ro.sergiu.finder.filters.Operator;
import ro.sergiu.finder.models.columns.ColumnModel;
import ro.sergiu.finder.ui.SearchEngineSpecifications;

/**
 *
 * @author Toporjinschi
 */
public abstract class FinderModel {

    public class SearchTermColumn<M> {

        private final M mappingItem;
        private Operator operator = Operator.LIKE;

        public SearchTermColumn(M mappingItem, Operator operator) {
            this.mappingItem = mappingItem;
            this.operator = operator;
        }
        public SearchTermColumn(M mappingItem) {
            this.mappingItem = mappingItem;
        }

        public M getMappingItem() {
            return mappingItem;
        }

        public Operator getOperator() {
            return operator;
        }

    }
    protected List<ColumnModel> visibleFields = null;

    /**
     * Filter list objects. <br/>
     * This is optional, if you don't want additional filters, do not override
     *
     * @return list of {@link FilterPanelUI}
     * @see FilterPanelUI
     */
    public List<FilterPanelUI> getFilters() {
        return new ArrayList<FilterPanelUI>();
    }

    /**
     * @return true if there will be some filters displayed
     */
    public final boolean hasFilters() {
        return getFilters().isEmpty();
    }

    /**
     * Override this method if you want to customized the columns in selection grid.<br/>
     *
     * @return a list of {@link ColumnModel} with the columns which will be displayed
     * @see ColumnModel
     */
    public List<ColumnModel> getColumns() {
        return new ArrayList<ColumnModel>();
    }

    /**
     * @return the list of visible columns in table see {@link ColumnModel}
     */
    public final List<ColumnModel> getVisibleColumns() {
        if (visibleFields != null) {
            return visibleFields;
        }
        visibleFields = new ArrayList<ColumnModel>();
        if (getColumns().isEmpty()) {
            return visibleFields;
        }
        Iterator<ColumnModel> itSelFields = this.getColumns().iterator();
        while (itSelFields.hasNext()) {
            ColumnModel column = itSelFields.next();
            if (column.isVisible()) {
                visibleFields.add(column);
            }
        }
        return visibleFields;
    }

    public abstract <T> List<SearchTermColumn<T>> getSearchTermColumns();

    public abstract SearchEngineSpecifications getSearchEngine();
}
